import useToastMsg from '@/hooks/sysMsg'
const toastMsg=useToastMsg()
export default function useDealResult() {
  /**
   * 处理增、删、改调用接口的返回结果，当出现错误自动提示，否则返回成功的结果
   * @param err 错误
   * @param result 接口返回结果
   * @param resolve Promise回调
   */
  const checkResult = (err: any, result: any) => {
    if (!err && result && result.code === 0) {
      return result
    }
    if (err) {
      toastMsg.errorMsg(err)
    }
    return null
  }

  /**
   * 处理获取列表或者详情返回结果
   * @param err 错误
   * @param result 接口返回结果
   * @param resolve Promise回调
   */
  const dealResult = (err: any, result: any, extChangeDataFun?: any) => {
    if (!err && result && result.code === 0) {
      result = (extChangeDataFun && extChangeDataFun(result)) || result
      return result
    }
    if (err) {
      toastMsg.errorMsg(err)
    }
    return null
  }
  /**
   * 下载
   * @param err 错误
   * @param result 接口返回结果
   * @param resolve Promise回调
   */
  const dealResultFormData = (err: any, result: any, extChangeDataFun?: any) => {
    if (!err && result && !result.code) {
      result = (extChangeDataFun && extChangeDataFun(result)) || result
      return result
    }
    if (!err && result && result.code === 0) {
      result = (extChangeDataFun && extChangeDataFun(result)) || result
      return result
    }
    if (!err) {
      toastMsg.errorMsg(err || '下载失败！')
    }
    return null
  }
  return {
    checkResult,
    dealResult,
    dealResultFormData,
  }
}
